import {
  Navigate, //v6版本使用Navigate代替Redirect
  useRoutes
} from 'react-router-dom'
import { lazy } from 'react'
import { RouteObject } from 'react-router-dom'
import PrivateRoute from './PrivateRoute'

import Home from '@/pages/Home/index'
// const Home = lazy(() => import('@/pages/Home/index'))
const Question = lazy(() => import('@/pages/Question/index'))
const Video = lazy(() => import('@/pages/Video/index'))
const Profile = lazy(() => import('@/pages/Profile/index'))
const Login = lazy(() => import('@/pages/Login/index'))
const Layout = lazy(() => import('@/pages/Layout/index'))
const ProfileEdit = lazy(() => import('@/pages/Profile/Edit/index'))
const NotFound = lazy(() => import('@/pages/NotFound/index'))
const Chat = lazy(() => import('@/pages/Profile/Chat/index'))
const Article = lazy(() => import('@/pages/Article/index'))
const Search = lazy(() => import('@/pages/Search/index'))
const SerachResult = lazy(() => import('@/pages/Search/Result/index'))

export default function AppRoutes() {
  const routes: RouteObject[] = [
    // 根路由重定向
    {
      path: '/',
      element: <Navigate to="/layout"></Navigate>
    },
    // layou重定向
    {
      path: '/layout',
      element: <Navigate to="/layout/home"></Navigate>
    },
    {
      path: '/layout',
      element: <Layout></Layout>,
      children: [
        // 首页
        {
          path: '/layout/home',
          element: <Home></Home>
        },
        // 问答页面
        {
          path: '/layout/question',
          element: <Question></Question>
        },
        // 视频页面
        {
          path: '/layout/video',
          element: <Video></Video>
        },
        // 个人资料页面
        {
          path: '/layout/profile',
          element: (
            <PrivateRoute>
              <Profile></Profile>
            </PrivateRoute>
          )
        }
      ]
    },
    // 登录页面
    {
      path: '/login',
      element: <Login></Login>
    },
    // 编辑个人资料页面
    {
      path: '/profile/edit',
      element: (
        <PrivateRoute>
          <ProfileEdit></ProfileEdit>
        </PrivateRoute>
      )
    },
    // 聊天页面
    {
      path: '/chat',
      element: <Chat></Chat>
    },
    // 文章页面
    {
      path: '/article/:id',
      element: <Article></Article>
    },
    // 搜索页面
    {
      path: '/search',
      element: <Search></Search>
    },
    // 搜索结果页面
    {
      path: '/search/result',
      element: <SerachResult></SerachResult>
    },
    // 通配符重定向到404路由
    {
      path: '*',
      element: <Navigate to="/404"></Navigate>
    },
    // 404路由
    {
      path: '/404',
      element: <NotFound></NotFound>
    }
  ]

  return useRoutes(routes, location)
}
